package light.mvc.controller.books;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import light.mvc.utils.FileUploadListener;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("ebookHelper")
public class BooksControllerHelper {
	@RequestMapping({ "/ProgressServlet.do" })
	public void progressServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();

		HttpSession session = request.getSession(true);
		if (session == null) {
			out.println("Sorry, session is null"); // just to be safe
			return;
		}

		FileUploadListener uploadProgressListener = (FileUploadListener) session.getAttribute("uploadProgressListener");
		if (uploadProgressListener == null) {
//			out.println("Progress listener is null");
			return;
		}
		int ret = uploadProgressListener.getPercentDone();
		System.out.println("new ->:" + String.valueOf(ret));
		out.println(ret);
	}
}